﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;

namespace GoShopping.BLL.SysInfo
{
    /// <summary>
    /// 地理位置维护
    /// 省、市、区
    /// </summary>
    public class AreaControl
    {
        #region Edit
        /// <summary>
        /// 添加一个省
        /// 注意：ID字段为自增字段，不需要赋值
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public bool AddProvince(string pName)
        {
            int i = Databases.DBGoShopping.Select(GoShoppingNBear.Province, GoShoppingNBear.Province.ID.Count()).Where(
                GoShoppingNBear.Province.Name == pName.Trim()).ToScalar<int>();
            if (i > 0)
            {
                return false;
            }
            else
            {
                i = Databases.DBGoShopping.Insert(GoShoppingNBear.Province).AddColumn(
                    GoShoppingNBear.Province.Name, pName).Execute();
                if (i > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 添加一个City数据
        /// 注意：ID字段为自增字段，不需要赋值
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public bool AddCity(City c)
        {
            //先查看有没有对应的省
            int i = Databases.DBGoShopping.Select(GoShoppingNBear.Province).Where(
                GoShoppingNBear.Province.ID == c.ProvinceID).ToScalar<int>();
            if (i < 0)
            {
                return false;
            }
            i = Databases.DBGoShopping.Insert(GoShoppingNBear.City).AddColumn(
                GoShoppingNBear.City.Name, c.Name).AddColumn(
                GoShoppingNBear.City.ProvinceID, c.ProvinceID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 添加一个County
        /// </summary>
        /// <param name="county"></param>
        /// <returns></returns>
        public bool AddCounty(County county)
        {
            int i = Databases.DBGoShopping.Select(GoShoppingNBear.City).Where(
                GoShoppingNBear.City.ID == county.CityID).ToScalar<int>();
            if (i < 0)
            {
                return false;
            }
            i = Databases.DBGoShopping.Insert(GoShoppingNBear.County).AddColumn(
               GoShoppingNBear.County.Name, county.Name).AddColumn(
               GoShoppingNBear.County.CityID, county.CityID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个省
        /// </summary>
        /// <remarks>注意：删除的同时，省下面的数据都会被删除.</remarks>
        /// <param name="pID"></param>
        /// <returns></returns>
        public void DelProvince(int pID)
        {
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
            {
                IList<City> cityList = Databases.DBGoShopping.Select(GoShoppingNBear.City).Where(
                    GoShoppingNBear.City.ProvinceID == pID).ToList<City>();
                foreach (City c in cityList)
                {
                    DelCity(c.ID);
                }

                Databases.DBGoShopping.Delete(GoShoppingNBear.Province).Where(
                   GoShoppingNBear.Province.ID == pID).Execute();
                scope.Complete();
            }
        }

        /// <summary>
        /// 删除CITY
        /// </summary>
        /// <param name="cityID"></param>
        public void DelCity(int cityID)
        {
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
            {
                IList<County> countyList = Databases.DBGoShopping.Select(GoShoppingNBear.County).Where(
                    GoShoppingNBear.County.CityID == cityID).ToList<County>();
                foreach (County c in countyList)
                {
                    DelCounty(c.ID);
                }

                Databases.DBGoShopping.Delete(GoShoppingNBear.City).Where(
                   GoShoppingNBear.City.ID == cityID).Execute();
                scope.Complete();
            }
        }

        /// <summary>
        /// 删除County
        /// </summary>
        /// <param name="countyID"></param>
        /// <returns></returns>
        public bool DelCounty(int countyID)
        {
            int i = Databases.DBGoShopping.Delete(GoShoppingNBear.County).Where(
                           GoShoppingNBear.County.ID == countyID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region Select
        /// <summary>
        /// 获取所有Province
        /// </summary>
        public IList<Province> GetAllProvince()
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.Province).ToList<Province>();
        }

        /// <summary>
        /// 获取省的City
        /// </summary>
        /// <param name="procinceid"></param>
        public IList<City> GetProvinceCity(int procinceid)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.City).Where(GoShoppingNBear.City.ProvinceID == procinceid).ToList<City>();
        }

        /// <summary>
        /// 获取市的County
        /// </summary>
        /// <param name="cityID"></param>
        public IList<County> GetCityCounty(int cityID)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.County).Where(GoShoppingNBear.County.CityID == cityID).ToList<County>();
        }

        public Province GetProvinceByID(int procinceid)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.Province).Where(
                GoShoppingNBear.Province.ID == procinceid).ToSingleObject<Province>();
        }

        public City GetCityByID(int cityid)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.City).Where(
                GoShoppingNBear.City.ID == cityid).ToSingleObject<City>();
        }

        public County GetCountyByID(int countyid)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.County).Where(
                GoShoppingNBear.County.ID == countyid).ToSingleObject<County>();
        }
        #endregion
    }
}
